﻿using Autofac;
using Infrastructure.UniOfWorkBase;
using MassTransit;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Infrastructure.DomainEvent
{
    public abstract class IDomainEventHandler<TDomainEvent> : MassTransit.IConsumer<TDomainEvent>
            where TDomainEvent :  DomainEventBase
    {
        public virtual Task Consume(ConsumeContext<TDomainEvent> context)
        {
            return ServiceProvide.DoLifeScope(ls =>
            {
                ls.InjectProperties(this);
                return HandleAysnc(context.Message);
            });
        }

        public abstract Task HandleAysnc(TDomainEvent domainEvent, CancellationToken cancellationToken = default);
    }
}
